﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HengTong.Model._Admin;
using HengTong.Model.Db;
using HengTong.Model.Db.Other;
using HengTong.ViewModel.Visitor.SysVisitorRecordRequestBillVms;
using Microsoft.AspNetCore.Mvc;
using TiaoTaoHelper.Models;
using TiaoTaoHelper.Wtm.Attributes;
using TiaoTaoHelper.Wtm.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace HengTong.Areas.Visitor.Controllers
{
    [Area("Visitor")]
    [AuthorizeJwtWithCookie]
    [ActionDescription("访客申请")]
    [ApiController]
    [Route("api/SysVisitorRecordRequestBill")]
    public partial class SysVisitorRecordRequestBillController : BaseApiController
    {
        [ActionDescription("Sys.Search")]
        [HttpPost("Search")]
        public IActionResult Search(SysVisitorRecordRequestBillSearcher searcher)
        {
            if (ModelState.IsValid)
            {
                var vm = Wtm.CreateVM<SysVisitorRecordRequestBillListVm>(passInit: true);
                vm.Searcher = searcher;
                return Ok(vm.GetJsonForApi());
            }
            else
            {
                return BadRequest(ModelState.GetErrorJson());
            }
        }


        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public SysVisitorRecordRequestBillVm Get(string id)
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillVm>(id);
            return vm;
        }


        [ActionDescription("Sys.Create")]
        [HttpPost("Add")]
        [PreventDuplicateSubmit]
        public async Task<IActionResult> Add(SysVisitorRecordRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [ActionDescription("Sys.Edit")]
        [HttpPut("Edit")]
        public async Task<IActionResult> Edit(SysVisitorRecordRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [HttpPost("[action]")]
        [ActionDescription("流程审批")]
        public async Task<ActionResult> Approve(SysVisitorRecordRequestBillVm vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.ApproveAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }


        [HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillBatchVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }

            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }


        [ActionDescription("Sys.Export")]
        [HttpPost("ExportExcel")]
        public IActionResult ExportExcel(SysVisitorRecordRequestBillSearcher searcher)
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillListVm>();
            vm.Searcher = searcher;
            vm.SearcherMode = ListVMSearchModeEnum.Export;
            return vm.GetExportData();
        }


        [ActionDescription("Sys.CheckExport")]
        [HttpPost("ExportExcelByIds")]
        public IActionResult ExportExcelByIds(string[] ids)
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillListVm>();
            if (ids != null && ids.Any())
            {
                vm.Ids = new List<string>(ids);
                vm.SearcherMode = ListVMSearchModeEnum.CheckExport;
            }

            return vm.GetExportData();
        }


        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillImportVm>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }

            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }


        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(SysVisitorRecordRequestBillImportVm vm)
        {
            if (vm != null && (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData()))
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm?.EntityList?.Count ?? 0);
            }
        }


        [HttpGet("GetSysVisitorReasons")]
        public ActionResult GetSysVisitorReasons()
        {
            return Ok(DC.Set<SysVisitorReason>().GetSelectListItems(Wtm, x => x.ReasonName));
        }


        [HttpGet("GetFrameworkUsers")]
        public ActionResult GetFrameworkUsers()
        {
            return Ok(DC.Set<FrameworkUser>().GetSelectListItems(Wtm, x => x.Name));
        }


        [HttpGet("GetSysGroups")]
        public ActionResult GetSysGroups()
        {
            //return Ok(DC.Set<SysGroup>().GetSelectListItems(Wtm, x => x.GroupName));
            //return Ok(DC.Set<SysGroup>().OrderBy(x => x.Order ?? 99.99m).Select(x => new { Value = x.ID, Text = x.GroupName }).ToList());
            return Ok(DC.Set<SysGroup>().Where(x => !x.IsDisabled).OrderBy(x => x.Order ?? 99.99m).Select(x => new { Value = x.ID, Text = x.GroupName }).ToList());
        }


        [ActionDescription("预测编码")]
        [HttpGet("PredictCode")]
        public async Task<Result<string>> PredictCode()
        {
            var vm = Wtm.CreateVM<SysVisitorRecordRequestBillVm>();
            return await vm.PredictCodeAsync();
        }
    }
}